package com.encryption.service.ca.util;

import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * 
 * @author user
 * @version $Id: CryptoUtil.java,v 0.1 2012-10-4 ����02:09:30 user Exp $
 */
public class CryptoUtil {
    /**
     * key ����ת����from Base64 to internal
     * @param sPub
     * @return
     * @throws Exception
     */
    static {
        if (Security.getProvider("BC") == null)
            Security.addProvider(new BouncyCastleProvider());
            
    }
    public static PublicKey getPublicKey(String sPub) throws Exception {
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.decode(sPub));
        PublicKey publicKey = KeyFactory.getInstance("RSA", "BC").generatePublic(publicKeySpec);
        return publicKey;
    }

    /**
     * key ����ת�� from Base64 to internal
     * @param sPrv
     * @return
     * @throws Exception
     */
    public static PrivateKey getPrivateKey(String sPrv) throws Exception {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decode(sPrv));
        KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
        PrivateKey privKey = kf.generatePrivate(keySpec);
        return privKey;
    }
    
    public static String getThumbPrint(X509Certificate cert) throws NoSuchAlgorithmException,
                                                            CertificateEncodingException {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] der = cert.getEncoded();
        md.update(der);
        byte[] digest = md.digest();
        return StringUtil.toUpperCase(hexify(digest));

    }
    
    public static String md5(String source) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        byte[] der = source.getBytes();
        md.update(der);
        byte[] digest = md.digest();
        return StringUtil.toUpperCase(hexify(digest));

    }

    public static String hexify(byte bytes[]) {

        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
                'e', 'f' };

        StringBuffer buf = new StringBuffer(bytes.length * 2);

        for (int i = 0; i < bytes.length; ++i) {
            buf.append(hexDigits[(bytes[i] & 0xf0) >> 4]);
            buf.append(hexDigits[bytes[i] & 0x0f]);
        }

        return buf.toString();
    }
    
    public static void main(String[] args) {
		System.out.println(md5("test"));
	}


}
